package com.meirengu.webview.service.impl;

import com.meirengu.common.RedisClient;
import com.meirengu.service.impl.BaseServiceImpl;
import com.meirengu.utils.DateUtils;
import com.meirengu.webview.common.Constants;
import com.meirengu.webview.common.UserChannelEnum;
import com.meirengu.webview.dao.KwInterfaceConfigDao;
import com.meirengu.webview.model.KwInterfaceConfig;
import com.meirengu.webview.model.KwPpdFilter;
import com.meirengu.webview.model.User;
import com.meirengu.webview.model.UserInfo;
import com.meirengu.webview.service.KwPpdFilterService;
import com.meirengu.webview.service.KwPpdPushService;
import com.meirengu.webview.service.UserInfoService;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

/**
 * KwPpdFilter服务实现层
 *
 * @author Mao
 * @create Mon May 07 20:07:49 CST 2018
 */
@Service
public class KwPpdFilterServiceImpl extends BaseServiceImpl<KwPpdFilter> implements KwPpdFilterService {

    @Autowired
    private RedisClient redisClient;

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private KwInterfaceConfigDao kwInterfaceConfigDao;

    @Autowired
    private KwPpdPushService kwPpdPushService;


    Logger logger = LoggerFactory.getLogger(KwPpdFilterServiceImpl.class);

    /**
     * 发送拍拍贷接口
     * 过滤条件
     * 1身份证
     *
     * @param userInfo
     * @param user
     * @throws Exception
     */
    public boolean sendPpdai(UserInfo userInfo, User user) throws Exception {

        boolean sendFlag = false;
        try {
            String ppdaiIol = redisClient.get("iol-ppdai");
            if ("N".equals(ppdaiIol)) {
                String id = user.getId();
                String name = user.getName();
                int age = 25;
                Date brithdayDate = userInfo.getBirthday();
                age = DateUtils.getAge(brithdayDate);
                String mobile = user.getMobile();
                String idcard = userInfo.getIdCard();
                String city = userInfo.getScity();
                BigDecimal expectAmount = userInfo.getExpectAmount();

                if (StringUtils.isNotBlank(idcard)) {
                    KwPpdFilter kwPpdFilter = new KwPpdFilter();
                    kwPpdFilter.setUserId(id);
                    kwPpdFilter.setMobile(mobile);
                    kwPpdFilter.setChannelCode("ppdai");
                    kwPpdFilter.setChannelName("拍拍贷");

                    KwInterfaceConfig kwInterfaceConfig = kwInterfaceConfigDao.findByCode("ppdai");

                    boolean dataFlag = true;
                    if (kwInterfaceConfig != null) {
                        if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitAge())) {
                            if (age < kwInterfaceConfig.getMinAge().intValue() || age > kwInterfaceConfig.getMaxAge().intValue()) {
                                dataFlag = false;
                                kwPpdFilter.setSuited("N");
                                kwPpdFilter.setCode(UserChannelEnum.FAIL_AGE.getValue());
                                kwPpdFilter.setRefuseMsg("用户年龄不符合：" + age);
                            }
                        }
                        if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitArea())) {
                            city = city.replace("市", "");
                            String cityRange = kwInterfaceConfig.getAreaValue();
                            if (!cityRange.contains(city)) {
                                dataFlag = false;
                                kwPpdFilter.setSuited("N");
                                kwPpdFilter.setCode(UserChannelEnum.FAIL_AREA.getValue());
                                kwPpdFilter.setRefuseMsg("用户所在城市不满足：" + city);
                            }
                        }
                        if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitAmount())) {
                            if (expectAmount.intValue() < kwInterfaceConfig.getMinAmount() || expectAmount.intValue() > kwInterfaceConfig.getMaxAmount()) {
                                dataFlag = false;
                                kwPpdFilter.setSuited("N");
                                kwPpdFilter.setCode(UserChannelEnum.FAIL_LOW_AMOUNT.getValue());
                                kwPpdFilter.setRefuseMsg("申请贷款额度小于" + kwInterfaceConfig.getMinAmount() + "w：" + expectAmount);
                            }
                        }
                        if (dataFlag) {
                            Integer code = kwPpdPushService.applyLoan(mobile, name, idcard, city);
                            kwPpdFilter.setSuited("Y");
                            kwPpdFilter.setCode(UserChannelEnum.PASS.getValue());
                            /*推送接口后修改限制数量*/
                            userInfoService.updateLimitNum(code, "ppdai", user);
                            if (code.intValue() == Constants.BUSINESS_STATUS_SUCCESS) {
                                sendFlag = true;
                            } else {
                                sendPpdaiBySleepingUser(userInfo, user);
                            }
                        }
                    }
                    insert(kwPpdFilter);
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(ExceptionUtils.getFullStackTrace(e));
        }
        return sendFlag;
    }


    /**
     * 发送拍拍贷接口
     * 过滤条件
     * 1身份证
     *
     * @param userInfo
     * @param user
     * @throws Exception
     */
    public boolean sendPpdaiBySleepingUser(UserInfo userInfo, User user) throws Exception {

        boolean sendFlag = false;
        try {
            String id = user.getId();
            String name = user.getName();
            String mobile = user.getMobile();
            String idcard = userInfo.getIdCard();

            if (StringUtils.isNotBlank(idcard)) {
                KwPpdFilter kwPpdFilter = new KwPpdFilter();
                kwPpdFilter.setUserId(id);
                kwPpdFilter.setMobile(mobile);
                kwPpdFilter.setChannelCode("ppdsu");
                kwPpdFilter.setChannelName("拍拍贷沉睡用户");

                Integer code = kwPpdPushService.applyLoanBySleepingUser(mobile, name, idcard);
                kwPpdFilter.setSuited("Y");
                kwPpdFilter.setCode(UserChannelEnum.PASS.getValue());
                /*推送接口后修改限制数量*/
                userInfoService.updateLimitNum(code, "ppdsu", user);
                if (code.intValue() == Constants.BUSINESS_STATUS_SUCCESS) {
                    sendFlag = true;
                }
                insert(kwPpdFilter);
            }

        } catch (Exception e) {
            e.printStackTrace();
            logger.error(ExceptionUtils.getFullStackTrace(e));
        }
        return sendFlag;
    }

    /**
     * 旧数据
     * 发送拍拍贷接口
     * 过滤条件
     * 1身份证
     *
     * @param userInfo
     * @param user
     * @throws Exception
     */
    public boolean sendPpdaiBySleepingUser2(UserInfo userInfo, User user) throws Exception {

        boolean sendFlag = false;
        try {
            String id = user.getId();
            String name = user.getName();
            String mobile = user.getMobile();
            String idcard = userInfo.getIdCard();

            if (StringUtils.isNotBlank(idcard)) {
                KwPpdFilter kwPpdFilter = new KwPpdFilter();
                kwPpdFilter.setUserId(id);
                kwPpdFilter.setMobile(mobile);
                kwPpdFilter.setChannelCode("ppdsu2");
                kwPpdFilter.setChannelName("拍拍贷沉睡用户2");

                Integer code = kwPpdPushService.applyLoanBySleepingUser2(mobile, name, idcard);
                kwPpdFilter.setSuited("Y");
                kwPpdFilter.setCode(UserChannelEnum.PASS.getValue());
                /*推送接口后修改限制数量*/
                userInfoService.updateLimitNum(code, "ppdsu2", user);
                if (code.intValue() == Constants.BUSINESS_STATUS_SUCCESS) {
                    sendFlag = true;
                }
                insert(kwPpdFilter);
            }

        } catch (Exception e) {
            e.printStackTrace();
            logger.error(ExceptionUtils.getFullStackTrace(e));
        }
        return sendFlag;
    }
}
